import SwiftUI
import SwiftUIIntrospect

struct ScrollViewShowcase: View {
	@State var receiverScrollViewFound: Bool = false
	@State var ancestorScrollViewFound: Bool = false

	var body: some View {
		VStack(spacing: 40) {
			ScrollView {
				Text("Default")
					.frame(maxWidth: .infinity)
					.lineLimit(1)
					.minimumScaleFactor(0.5)
					.padding(.horizontal, 12)
			}

			ScrollView {
				Text(".introspect(.scrollView, ...)")
					.frame(maxWidth: .infinity)
					.lineLimit(1)
					.minimumScaleFactor(0.5)
					.padding(.horizontal, 12)
					.font(.system(.subheadline, design: .monospaced))
			}
			.background {
				if receiverScrollViewFound {
					Color(.cyan)
				}
			}
			#if os(iOS) || os(tvOS) || os(visionOS)
			.introspect(
				.scrollView,
				on: .iOS(.v15, .v16, .v17, .v18, .v26), .tvOS(.v15, .v16, .v17, .v18, .v26), .visionOS(.v1, .v2, .v26)
			) { _ in
				DispatchQueue.main.async {
					receiverScrollViewFound = true
				}
			}
			#elseif os(macOS)
			.introspect(.scrollView, on: .macOS(.v12, .v13, .v14, .v15, .v26)) { scrollView in
				DispatchQueue.main.async {
					receiverScrollViewFound = true
				}
			}
			#endif

			ScrollView {
				Text(".introspect(.scrollView, ..., scope: .ancestor)")
					.frame(maxWidth: .infinity)
					.lineLimit(1)
					.minimumScaleFactor(0.5)
					.padding(.horizontal, 12)
					.font(.system(.subheadline, design: .monospaced))
					#if os(iOS) || os(tvOS) || os(visionOS)
					.introspect(
						.scrollView,
						on: .iOS(.v15, .v16, .v17, .v18, .v26), .tvOS(.v15, .v16, .v17, .v18, .v26), .visionOS(.v1, .v2, .v26),
						scope: .ancestor
					) { _ in
						DispatchQueue.main.async {
							ancestorScrollViewFound = true
						}
					}
					#elseif os(macOS)
					.introspect(.scrollView, on: .macOS(.v12, .v13, .v14, .v15, .v26), scope: .ancestor) { scrollView in
						DispatchQueue.main.async {
							ancestorScrollViewFound = true
						}
					}
					#endif
			}
			.background {
				if ancestorScrollViewFound {
					Color(.cyan)
				}
			}
		}
	}
}
